package com.code.bolg.controller;

import com.code.bolg.bean.Article;
import com.code.bolg.bean.Category;
import com.code.bolg.bean.User;
import com.code.bolg.enums.DelFlagEnum;
import com.code.bolg.output.ArticleOutPut;
import com.code.bolg.output.IndexDto;
import com.code.bolg.service.ArticleService;
import com.code.bolg.service.CategoryService;
import com.code.bolg.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * article
 *
 * @author  2018-10-12
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;
    @Autowired
    private UserService userService;
    @Autowired
    private CategoryService categoryService;
    /**
     * 新增或编辑
     */
    @PostMapping("/saveOrUpdate")
    public Object save(Article article) {
        if(article.getArticleId()!=null){
            article.setUpdateDate(new Date());
        }else if(article.getArticleId()==null){
            article.setCreateDate(new Date());
            article.setUpdateDate(article.getCreateDate());
            article.setFlag(DelFlagEnum.NOTDELETE.getCode());
        }
        Article save = articleService.save(article);
        ArticleOutPut outPut = new ArticleOutPut();
        BeanUtils.copyProperties(save,outPut);
        return outPut;
    }

    /**
     * 删除
     */
    @GetMapping("/delete")
    public Object delete(int id) {
        return articleService.deleteById(id);
    }

    /**
     * 查询
     */
    @GetMapping("/find")
    public Object find(int id) {
        Article article = articleService.findById(id);
        return article;
    }

    /**
     * 分页查询
     */
    @PostMapping("/list")
    public Object list(Article article,
                       @RequestParam(required = false, defaultValue = "0") int page,
                       @RequestParam(required = false, defaultValue = "10") int size) {
        Page<Article> all = articleService.findAll(article, page - 1 == -1 ? 0 : page - 1, size);
        List<ArticleOutPut> ouputList = new ArrayList<>();


        //数据库查询
        List<User> userList = userService.findAll();
        List<Category> categoryList = categoryService.findAll();
        //ariaceList
        for (Article model : all.getContent()) {
            ArticleOutPut outPut = new ArticleOutPut();
            BeanUtils.copyProperties(model, outPut);

            Optional<User> userOptional = userList.stream().filter(item -> item.getUserId() == model.getUserId()).findFirst();
            Optional<Category> categoryOptional = categoryList.stream().filter(item -> item.getCategoryId() == model.getCategoryId()).findFirst();

            //设值用户信息
            if (userOptional.isPresent()) {
                User user = userOptional.get();
                outPut.setUserId(user.getUserId());
                outPut.setAnthor(user.getUsername());
            }

            //设值分类信息
            if (categoryOptional.isPresent()) {
                Category category = categoryOptional.get();
                outPut.setCategoryId(category.getCategoryId());
                outPut.setCategoryName(category.getCategoryName());
            }
            ouputList.add(outPut);
        }



        IndexDto< ArticleOutPut>  dto =  new IndexDto();
        dto.setCount(Integer.valueOf (String.valueOf(all.getTotalElements())));
        dto.setList(ouputList);
        return dto;
    }

}

